<?php

class Light_Controller
{
    /**
     * Banned actions
     *
     * @var array
     */
	protected $_bannedActions = array();

	/**
	 * Request
	 *
	 * @var Light_Request
	 */
	protected $_request = null;

	/**
	 * Router
	 *
	 * @var Light_Router_Rewrite
	 */
	protected $_router = null;

	/**
	 * Controller name
	 *
	 * @var string
	 */
	protected $_controllerName = null;

	/**
	 * Action name
	 *
	 * @var string
	 */
	protected $_actionName = null;

	/**
	 * Module name
	 * @var string
	 */
	protected $_moduleName = null;

	/**
	 * is disabled layout ?
	 *
	 * @var bool
	 */
	protected $_isDisableLayout = false;

	/**
	 * is disabled view ?
	 *
	 * @var bool
	 */
	protected $_isDisableView = false;

	/**
	 * Context
	 *
	 * @var string
	 */
	protected $_context = 'html';

	/**
	 * @var Light_View
	 */
	public $view;

	/**
	 * @var Light_Layout
	 */
	public $layout;

	public function __construct()
	{

	}

	/**
	 * Method for some initialisation
	 */
	public function init() {}

	/**
	 * Set request
	 *
	 * @param Light_Request $request
	 * @return Light_Controller object
	 */
	public function setRequest(Light_Request $request)
	{
	    $this->_request = $request;

	    return $this;
	}

	/**
	 * Get request
	 *
	 * @return Light_Request
	 */
	public function getRequest()
	{
	    return $this->_request;
	}

	/**
     * @return Light_Router_Rewrite
     */
    public function getRouter ()
    {
        return $this->_router;
    }

	/**
	 * Set router
	 *
	 * @param Light_Router_Rewrite $_router
	 * @return Light_Controller object
	 */
    public function setRouter (Light_Router_Rewrite $_router)
    {
        $this->_router = $_router;

        return $this;
    }

	/**
	 * Set module name
	 *
	 * @param string $moduleName
	 * @return Light_Controller object
	 */
	public function setModuleName($moduleName)
	{
	    $this->_moduleName = $moduleName;

	    return $this;
	}

	/**
	 * Get module name
	 *
	 * @return string
	 */
	public function getModuleName()
	{
	    return $this->_moduleName;
	}

	/**
	 * Set controller name
	 *
	 * @param string $controllerName
	 * @return Light_Controller object
	 */
	public function setControllerName($controllerName)
	{
		$this->_controllerName = $controllerName;

		return $this;
	}

	/**
	 * Get controller name
	 *
	 * @return string
	 */
	public function getControllerName()
	{
		return $this->_controllerName;
	}


	/**
	 * Set action name
	 *
	 * @param string $actionName
	 * @return Light_Controller object
	 */
	public function setActionName($actionName)
	{
		$this->_actionName = $actionName;

		return $this;
	}

	/**
	 * Get action name
	 *
	 * @return string
	 */
	public function getActionName()
	{
		return $this->_actionName;
	}

	/**
	 * Disable layout
	 *
	 * @return Light_Controller object
	 */
	public function disableLayout()
	{
	    $this->_isDisableLayout = true;

	    return $this;
	}


	/**
	 * Enable layout
	 *
	 * @return Light_Controller object
	 */
	public function enableLayout()
	{
	    $this->_isDisableLayout = false;

	    return $this;
	}

	/**
	 * Disable view
	 *
	 * @return Light_Controller object
	 */
	public function disableView()
	{
		$this->_isDisableView = true;

		return $this;
	}

	/**
	 * Enable view
	 *
	 * @return Light_Controller object
	 */
	public function enableView()
	{
		$this->_isDisableView = false;

		return $this;
	}

	/**
	 * Set context
	 *
	 * @param string $context
	 * @return Light_Controller object
	 */
	public function setContext($context)
	{
	    $this->_context = $context;

	    return $this;
	}

	/**
	 * Get ccontext
	 *
	 * @return string
	 */
	public function getContext()
	{
	    return $this->_context;
	}

	/**
	 * Display all
	 *
	 */
	public function display()
	{
	    switch ($this->getContext()) {
	        case 'json' :
	            header('Content-Type: application/json; charset=utf-8');
	            $this->disableView();
	            $this->disableLayout();
	            if (isset($this->output)) {
	                echo json_encode($this->output);
	            }
	            break;
	        case 'xml' :
	            header('Content-Type: text/xml; charset=utf-8');
	            echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
	            if (isset($this->output)) {
	                echo $this->output;
	            } else {
	                echo '<empty>No output</empty>';
	            }
	            $this->disableView();
	            $this->disableLayout();
	            break;
	        default :
	            header('Content-Type: text/html; charset=utf-8');
	        break;
	    }

		$this->getLayout()->content = '';

		if (!$this->_isDisableView) {
			$this->getLayout()->content = $this->getView()->render();
		}

		if ($this->_isDisableLayout) {
			echo $this->getLayout()->content;
		} else {
			echo $this->getLayout()->render();
		}
	}

	/**
     * @return the $_bannedActions
     */
    public function getBannedActions ()
    {
        return $this->_bannedActions;
    }

	/**
     * @param multitype:string  $_bannedActions
     */
    public function setBannedActions ($_bannedActions)
    {
        $this->_bannedActions = $_bannedActions;

        return $this;
    }

	/**
     * @return Light_View
     */
    public function getView ()
    {
        return $this->view;
    }

	/**
     * @param Light_View $view
     * @return Light_Controller
     */
    public function setView ($view)
    {
        $this->view = $view;

        return $this;
    }

	/**
     * @return Light_Layout
     */
    public function getLayout ()
    {
        if ($this->layout === null) {
            $this->layout = new Light_Layout($this);
        }

        return $this->layout;
    }

	/**
     * @param Light_Layout $layout
     * @return Light_Controller
     */
    public function setLayout ($layout)
    {
        $this->layout = $layout;

        return $this;
    }

    /**
     * @param string $key
     * @param string $default
     * @return string
     */
    protected function _getParam($key, $default = null)
    {
        return $this->_request->getParam($key, $default);
    }

    /**
     * @param string $key
     * @param string $value
     * @return Light_Controller
     */
    protected function _setParam($key = null, $value = null)
    {
       $this->_request->setParam($key, $value);

       return $this;
    }

    /**
     * @param string $event
     * @param mixed $params
     * @return boolean
     */
    public function triggerEvent($event, $params = null)
    {
        return Light_Core::getEventManager()->trigger($event, $params);
    }

    /**
     * @param string $name
     * @param boolean $new
     * @throws Light_Exception
     * @return Light_Service_Proxy
     */
    public function getService($name, $new = false)
    {
        return Light_Core::getServiceManager()->get($name, $new);
    }
}